package com.cszy.yydqbfq.ui.activity

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import com.airbnb.lottie.LottieDrawable
import com.banmen.ad.mds.views_notload.DoubleSplashAd
import com.bytedance.sdk.openadsdk.CSJSplashAd
import com.cszy.yydqbfq.databinding.ActivitySplashBinding
import com.cszy.yydqbfq.base.BaseVMActivity
import com.cszy.yydqbfq.ext.disableBackPress

import com.cszy.yydqbfq.utils.SpUtils
import com.cszy.yydqbfq.viewmodel.MainViewModel
import com.hlyj.http.ad.NewGroMoreFeedAdManager
import com.hlyj.http.base.tool.lib_hlyj_base.Constants
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseAppConstans
import com.hlyj.http.base.tool.lib_hlyj_base.utils.BaseModuleSpUtils
import com.hlyj.http.base.tool.lib_hlyj_base.utils.JsonUtil


import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.manager.AdManager
import com.hnyyac.ad.mds.manager.AdManager.AdLoadListener

import com.hnyyac.ad.mds.utils.BaseAdConstants

import com.hnyyac.ad.mds.views_notload.IAdviewNotLoadSplashListener
import com.ok.common.api.GlobalApi

class HotSplashActivity : BaseVMActivity<MainViewModel, ActivitySplashBinding>(), IAdViewListener {
    private lateinit var splashAd: DoubleSplashAd


    override fun initView(savedInstanceState: Bundle?) {
        disableBackPress()
        splashAd = DoubleSplashAd(this, object : IAdviewNotLoadSplashListener {
            override fun onTrigger(adUnitId: String) {
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.TRIGGER,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ, 1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance()
                            .getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to Constants.SPLASH,
                        Constants.PLACEMENTID to adUnitId,
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                //  Log.e("ProductEnvConstance", "onTrigger${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdShow(ad: CSJSplashAd, adUnitId: String, position: Int) {
                //  Log.e("splash", "onAdShow${ad.mediationManager.showEcpm.sdkName}")

                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.SHOW,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ, 1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            Constants.HARDWAREINFO
                        ),
                        Constants.ADFORMAT to Constants.SPLASH,
                        Constants.PLACEMENTID to adUnitId,
                        Constants.ADSOURCEID to ad.mediationManager.showEcpm.slotId,
                        Constants.OCCODE to position,
                        Constants.SHOWID to ad.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to ad.mediationManager.showEcpm.ecpm,
                        Constants.TIME to System.currentTimeMillis().toString(),
                        Constants.DATA to JsonUtil.parseStringToJsonWithMsg(ad.mediationManager.showEcpm.sdkName)//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdClick(ad: CSJSplashAd, adUnitId: String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.CLICK,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ, 1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            Constants.HARDWAREINFO
                        ),
                        Constants.ADFORMAT to Constants.SPLASH,
                        Constants.PLACEMENTID to adUnitId,
                        Constants.ADSOURCEID to ad.mediationManager.showEcpm.slotId,
                        Constants.OCCODE to ocCodee,
                        Constants.SHOWID to ad.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to ad.mediationManager.showEcpm.ecpm,
                        Constants.TIME to System.currentTimeMillis().toString(),
                        //   Constants.DATA to JsonUtil.parseStringToJsonWithMsg("这是自定义"),//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onAdClose(ad: CSJSplashAd, adUnitId: String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.CLOSE,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ, 1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            Constants.HARDWAREINFO
                        ),
                        Constants.ADFORMAT to Constants.SPLASH,
                        Constants.PLACEMENTID to adUnitId,
                        Constants.ADSOURCEID to ad.mediationManager.showEcpm.slotId,
                        Constants.OCCODE to ocCodee,
                        Constants.SHOWID to ad.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to ad.mediationManager.showEcpm.ecpm,
                        Constants.TIME to System.currentTimeMillis().toString(),
                        //   Constants.DATA to JsonUtil.parseStringToJsonWithMsg("这是自定义"),//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
//                AdManager.loadChapin(
//                    this@HotSplashActivity,
//                    this@HotSplashActivity,
//                    object : AdLoadListener {
//                        override fun onAdLoadFailed(ad: IAdView?) {
//                            finish()
//
//                        }
//                    },onPutOccode = {
//                        BaseModuleSpUtils.getInstance().putInt(Constants.OCCODE, 12)
//                    })


            }

            override fun onError(msg: String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.FAIL,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ, 1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance().getString(
                            Constants.HARDWAREINFO
                        ),
                        Constants.ADFORMAT to Constants.SPLASH,
                        // Constants.PLACEMENTID to splash,
                        Constants.OCCODE to ocCodee,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        //  Constants.DATA to JsonUtil.parseStringToJsonWithMsg(code),
                        Constants.TIME to System.currentTimeMillis().toString()
                    )

                )
                GlobalApi.sendLog(logContent)


            }

            override fun onSuccess(ad: CSJSplashAd, adUnitId: String) {
                var ocCodee = BaseModuleSpUtils.getInstance().getInt(Constants.OCCODE, 0)
                val logContent = listOf(
                    mapOf(
                        Constants.TYPE to Constants.LOAD,
                        Constants.SEQ to BaseModuleSpUtils.getInstance().getInt(Constants.SEQ, 1),
                        Constants.HARDWAREINFO to BaseModuleSpUtils.getInstance()
                            .getString(Constants.HARDWAREINFO),
                        Constants.ADFORMAT to Constants.SPLASH,
                        Constants.PLACEMENTID to adUnitId,
                        Constants.ADSOURCEID to ad.mediationManager.showEcpm.slotId,
                        Constants.OCCODE to ocCodee,
                        Constants.SHOWID to ad.mediationManager.showEcpm.requestId,
                        // Constants.ECPM to BigDecimal(adView.getEcpm()).toPlainString()
                        Constants.ECPM to ad.mediationManager.showEcpm.ecpm,
                        Constants.TIME to System.currentTimeMillis().toString(),
                        //   Constants.DATA to JsonUtil.parseStringToJsonWithMsg("这是自定义"),//这里
                    )

                )
                // Log.e("ProductEnvConstance", "onAdShow${logContent}")
                GlobalApi.sendLog(logContent)
            }

            override fun onSkipped() {

            }

            override fun goMain() {
                AdManager.loadChapin_nocache(
                    this@HotSplashActivity,
                    21,
                    null,
                    object : AdLoadListener {
                        override fun onAdLoadFailed(ad: IAdView?) {

                        }
                    })
                finish()

            }
        })

    }

    override fun initData() {
        super.initData()
        Log.e("qin", "又進了HotSplashActivity")
        //重置所有tab 狀態為可點擊
        SpUtils.getInstance().putBoolean(BaseAppConstans.IS_show_TAB1, false)
        SpUtils.getInstance().putBoolean(BaseAppConstans.IS_show_TAB2, false)
        SpUtils.getInstance().putBoolean(BaseAppConstans.IS_show_TAB3, false)
        SpUtils.getInstance().putBoolean(BaseAppConstans.IS_show_TAB4, false)
        loadSplash()
    }


    @SuppressLint("SuspiciousIndentation")
    private fun loadSplash() {
        //  toast("???????????????")
        Log.e("qin", "loadSplash")
        BaseModuleSpUtils.getInstance().putInt(Constants.OCCODE, 11)
        //是否是归因用户加载不同的广告位

        Log.e("SplashActivity", "isMonitor==1")
        splashAd.loadDoubleSplashAd(
            primaryAdUnitId = BaseAdConstants.SPLASH_ID_1,
            secondaryAdUnitId = BaseAdConstants.SPLASH_ID_2,
            container = mBinding.contentLayout,
            timeout = 10000,
            position = 1,
            isHot = true
        )


    }

    override fun onTrigger(adView: IAdView) {

    }

    override fun onAdShow(adView: IAdView) {

    }

    override fun onAdClick(adView: IAdView) {

    }

    override fun onAdClose(adView: IAdView) {
        //finishAndLaunchOriginal()
        //  finish()
    }

    override fun onError(msg: String) {
        Log.e("HotSplashActivity", "onError${msg}")
    }

    override fun onLoadError(adView: IAdView) {
        Log.e("HotSplashActivity", "onLoadError")
    }

    override fun onSuccess(adView: IAdView) {

    }

    override fun onLoad(adView: IAdView) {

    }

    override fun onSkipped() {

    }

    override fun onNoAd() {

    }

    override fun onTimeOut() {
        Log.e("HotSplashActivity", "onTimeOut")
    }

    override fun onPause() {
        //   splashAd.onPause()
        super.onPause()
    }

    override fun onResume() {
        super.onResume()
        //  splashAd.onResume()
    }

    override fun onDestroy() {
        splashAd.onDestroy()
        mBinding.lottieView.stop()
        super.onDestroy()
    }


}